package com.newstart.springdatajpa.project.model.entity;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.hibernate.annotations.UpdateTimestamp;

import javax.persistence.*;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Article implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @Basic
    @Column(name = "title", nullable = true, length = -1)
    private String title;

    @Column(name = "project_id", nullable = false)
    private int projectId;

    @Column(name = "content", nullable = true)
    private String content;

    @CreationTimestamp
    @Column(name = "create_time", nullable = true)
    private Timestamp createTime;

    @UpdateTimestamp
    @Column(name = "update_time", nullable = true)
    private Timestamp updateTime;

    @OneToOne(mappedBy = "article",cascade = CascadeType.PERSIST)//mappedBy:主表,cascade:关联属性
    @JsonIgnoreProperties(value = "article")//中断级联死循环。
    private ArticleDetail articleDetail;

    @OneToMany(mappedBy = "article",cascade = CascadeType.PERSIST)
    @JsonIgnoreProperties(value = "article")
    private List<ArticleComment> articleCommentList;

    @Override
    public String toString() {
        return "Article{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", projectId=" + projectId +
                ", content='" + content + '\'' +
                ", createTime=" + createTime +
                ", updateTime=" + updateTime +
                '}';
    }
}
